//
//  ARMusicCell.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/29.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARMusicCell.h"
#import "ARAudioPlayDefault.h"

//#import "UIImageView+WebCache.h"

@interface ARMusicCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageVeiw;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *misicImageView;
@property (weak, nonatomic) IBOutlet UILabel *musicAutorLabel;
@property (weak, nonatomic) IBOutlet UILabel *musicITitle;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *likesLabel;
@property (weak, nonatomic) IBOutlet UILabel *viewsLabel;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hightConstraint;
@property (weak, nonatomic) IBOutlet UIButton *playButton;

@end

@implementation ARMusicCell

- (IBAction)shareButtonClick:(id)sender {
    
    if (self.ShareBtnBlock) {
        self.ShareBtnBlock(self.cateModel,sender);
    }
}


- (UIButton *) myLikeButton
{
    return self.likeButton;
    
}

- (IBAction)playButtonClick:(UIButton *)sender {
    
    self.playButton.selected = !self.playButton.selected;
    
    
    if (self.playButton.selected) {
        
        
        ARAudioPlayDefault *audioDefault =  [ARAudioPlayDefault defaultAudioPlay];
        
        audioDefault.title = self.cateModel.mediaModel.musicModel.title;
        audioDefault.author = self.cateModel.mediaModel.musicModel.author;
        audioDefault.isPlay = YES;
        //audioDefault.player.currentTime = 0;
        
        
        if (self.delegate) {
            [self.delegate musicCell:self WithCateModel:self.cateModel];
        }
        
    }else{
        
        
        /*
        ARSameDefault *sameDefault = [ARSameDefault defaultSame];
        [sameDefault.player pause];
         */
        
        
        ARAudioPlayDefault *audioDefault = [ARAudioPlayDefault defaultAudioPlay];
        [audioDefault.player pause];
        audioDefault.isPlay = NO;
        
        if (self.delegate) {
            [self.delegate musicCellPause];
        }
    }
}



- (void) setCateModel:(ARCate5Model *)cateModel
{
    _cateModel = cateModel;
    
    ARAudioPlayDefault *audioDefault = [ARAudioPlayDefault defaultAudioPlay];
    
    if ([audioDefault.title isEqualToString:cateModel.mediaModel.musicModel.title] && audioDefault.isPlay) {
        self.playButton.selected = YES;
    }
    else
    {
        self.playButton.selected = NO;
    
    }
    
    self.iconImageVeiw.image = nil;
    self.misicImageView.image = nil;
    
    self.iconImageVeiw.layer.cornerRadius = 18;
    self.iconImageVeiw.clipsToBounds = YES;
    [self.iconImageVeiw setImageWithURL:[NSURL URLWithString:cateModel.userModel.avatar]];
    self.nameLabel.text = cateModel.userModel.username;
    
    [self.misicImageView setImageWithURL:[NSURL URLWithString:cateModel.mediaModel.musicModel.cover]];
    self.musicITitle.text = cateModel.mediaModel.musicModel.title;
    self.musicAutorLabel.text = cateModel.mediaModel.musicModel.author;
    
    self.contentLabel.text = cateModel.txt;
    self.likesLabel.text = cateModel.likes.stringValue;
    self.viewsLabel.text = cateModel.views.stringValue;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    

    

}


- (IBAction)likeButtonClick:(id)sender {
    
    [self likeButtonAction];
    
}



- (void)awakeFromNib {
    // Initialization code
    
    [self.playButton setImage:[UIImage imageNamed:@"pause@2x"] forState:UIControlStateSelected];
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
